// pages/facility/facility.js
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
import {
  API_CONFIRM_BINDING_PASTE,
  API_CONFIRM_UNBINDING_PASTE
} from '../../api/index'

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    bindFacilityData: {},
    userInfo: {}, // 微信用户数据
    isVisitor: false,
    deviceId: '',
    deviceDatas: [], // 设备数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.nfc = wx.getNFCAdapter();
    const bindFacilityData = wx.getStorageSync('bindFacilityData'),
      isVisitor = wx.getStorageSync('isVisitor')
    this.setData({
      isVisitor,
      userInfo: wx.getStorageSync('userInfo'),
      bindFacilityData,
      deviceDatas: bindFacilityData.tppaste != 0 && !isVisitor ? [{
        tpcode: bindFacilityData.tpcode,
        tpname: bindFacilityData.tpname,
      }] : []
    })
    if(bindFacilityData.tppaste == 0 || isVisitor){
      this.startNFC()
    }
  },
  transName: function (str) {
    let head = "NFC-"
    if (str == null) {
      return head + '*'
    }
    if (str.length <= 4) {
      return head + str
    } else {
      return head + str.substring(str.length - 4, str.length)
    }
  },

  onUnload: async function () {
    wx.setStorageSync('bindFacilityData', {})
    wx.setStorageSync('isVisitor', false)
    app.stopDiscovery();
  },
  // 绑定体温贴
  onBingFacility(e) {
    console.log(e)
    const params = {
      open_id: wx.getStorageSync('openId'),
      username: this.data.isVisitor ? this.data.userInfo.nickName : this.data.bindFacilityData.username,
      idcardno: this.data.bindFacilityData.idcardno,
      tpname: e.currentTarget.dataset.item.tpname,
      tpcode: e.currentTarget.dataset.item.tpcode,
      tourist: this.data.bindFacilityData.sort == 8 || this.data.isVisitor ? 'Y' : '',
      userid: this.data.isVisitor ? 'undefined' : this.data.bindFacilityData.userid + ''
    }
    // 连接蓝牙设备
    // this.connetBlue(e.currentTarget.dataset.item.tpcode);
    API_CONFIRM_BINDING_PASTE(params).then(res => {
      if (res.code == 200) {
        wx.showToast({
          title: '绑定成功',
        })
        setTimeout(() => {
          wx.reLaunch({
            url: '/pages/index/index',
          })
        }, 1000)
      }
    })
  },

  // 解绑体温贴
  onUnBingFacility() {
    const params = {
      unbundlingType: 'Y',
      tourist: this.data.bindFacilityData.sort == 8 ? 'Y' : "",
      open_id: wx.getStorageSync('openId'),
      username: this.data.bindFacilityData.username,
      idcardno: this.data.bindFacilityData.idcardno,
      tpcode: this.data.bindFacilityData.tpcode,
    }
    API_CONFIRM_UNBINDING_PASTE(params).then(res => {
      if (res.code == 200) {
        wx.showToast({
          title: '解绑成功',
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 2000)
      }
    })
  },


  startNFC: async function () {
    this.setData({
      disCoverRes: [],
      deviceDatas:[]
    })

    app.onDiscoveredCallBack = async (NFCId) => {
      const {
        bindFacilityData
      } = this.data;
      NFCId === bindFacilityData.tpcode;
      console.log("唯一ID:", NFCId);
      const {
        deviceDatas
      } = this.data;
      const ids = deviceDatas.map(v => v.tpcode);
      if (ids.indexOf(NFCId) == -1) {
        deviceDatas.push({
          tpcode: NFCId,
          tpname: this.transName(NFCId)
        })
        this.setData({
          deviceDatas
        })
      }
    }
    app.startNFCServer();
  }

})